#include "b2UserDataManager.h"

b2UserDataManager::b2UserDataManager() {}
b2UserDataManager::~b2UserDataManager() {}


bool b2UserDataManager::has(string s) {
	return udh.find(s) != udh.end();
}

b2UserData* b2UserDataManager::get(string s) {
	if(has(s)) {
		return udh[s];
	} else {
		return NULL;
	}
}


b2Body* b2UserDataManager::getBody(string s) {
	if(has(s) && udh[s]->b2ObjectType == e_b2BodyType) {
		return (b2Body*)(udh[s]->b2Object);
	} else {
		return NULL;
	}
}



/* iffy */
/*
b2Joint* b2UserDataManager::getJoint(string s) {
	if(has(s) && (udh[s]->b2ObjectType == e_b2JointType || 
				  udh[s]->b2ObjectType == e_b2DistanceJointType ||
				  udh[s]->b2ObjectType == e_b2GearJointType ||
				  udh[s]->b2ObjectType == e_b2MouseJointType ||
				  udh[s]->b2ObjectType == e_b2PrismaticJointType ||
				  udh[s]->b2ObjectType == e_b2PulleyJointType ||
				  udh[s]->b2ObjectType == e_b2RevoluteJointType 
				  ) ) {
		return (b2Joint*)(udh[s]->b2Object);
	} else {
		return NULL;
	}
}
*/

/* iffy */
/*
b2Shape* b2UserDataManager::getShape(string s) {
	if(has(s) && (udh[s]->b2ObjectType == e_b2ShapeType || 
				  udh[s]->b2ObjectType == e_b2CircleShapeType || 
				  udh[s]->b2ObjectType == e_b2CircleShapeType) ) {
		return (b2Shape*)(udh[s]->b2Object);
	} else {
		return NULL;
	}
}
*/




void b2UserDataManager::add(string s, b2UserData* ud) {
	udh[s] = ud;

}



bool b2UserDataManager::remove(string s) {

        UserDataHash::iterator element = udh.find(s);
		
		if(element != udh.end()) {
			udh.erase(element);
			post("%s erased",s.c_str());
			return true;
		} else {
			error("could not erase %s",s.c_str());
		 
		} 
		
		
     
	return false;
}


void b2UserDataManager::reset() {
	
	// clear the hash
	udh.clear();
}